var express = require('express');
var router = express.Router();
const jwt = require('../jwt/index');
const {UsersModel} = require('../db');
let {objectValueAllEmpty} = require('../utils')

/**
 * 用户登录
 */
router.post('/login', function (req, res, next) {
  const {username, password} = req.body;

  UsersModel.findOne({
    username, password
  }, function (err, userInfo) {
    if (err) {
      res.send({
        errno: 201,
        message: err.message
      })
    } else if (userInfo === null) {
      res.send({
        errno: 1001,
        message: "对象不存在"
      })
    } else {
      // 用户信息转成token
      const token = jwt(userInfo);
      res.send({
        errno: 0,
        message: 'success',
        data: {
          token
        }
      })
    }
  })
});

/**
 * 根据token获取用户信息
 */
router.get('/info', function (req, res, next) {
  const userInfo = req.auth;
  res.send({
    errno: 0,
    message: '成功',
    data: {
      userInfo
    }
  })
})

/**
 * 注册
 */
router.post('/register', function (req, res, next) {
  // 检查每个参数是否有值
  if (objectValueAllEmpty(req.body)) {
    res.send({
      errno: 202,
      message: '参数错误'
    })
    return
  }
  const {username, password, secretSecurity, secretSecurityAnswer} = req.body;
  // 检查是否重复用户名
  UsersModel.findOne({username}, function (err, row) {
    if (row !== null) {
      res.send({
        errno: 1002,
        message: '用户名已存在'
      })
    } else {

      UsersModel.create({
        username, password, secretSecurity, secretSecurityAnswer
      }, function (err) {
        if (err) {
          res.send({
            errno: 201,
            message: err.message
          })
        } else {
          res.send({
            errno: 0,
            message: 'success'
          })
        }
      })
    }
  })
});

/**
 * 获取用户密保问题
 */
router.get('/secretSecurity', function (req, res) {
  // 检查每个参数是否有值
  if (objectValueAllEmpty(req.query)) {
    res.send({
      errno: 202,
      message: '参数错误'
    })
    return
  }
  const {username} = req.query;
  UsersModel.findOne({
    username
  }, function (err, userInfo) {
    if (err) {
      res.send({
        errno: 201,
        message: err.message
      })
    } else if (userInfo === null) { // 没查询到
      res.send({
        errno: 1003,
        message: "用户名不存在"
      })
    } else {
      // 返回用户密保问题
      res.send({
        errno: 0,
        message: 'success',
        data: {
          secretSecurity: userInfo.secretSecurity,
          secretSecurityAnswer: userInfo.secretSecurityAnswer
        }
      })
    }
  })
})

/**
 * 改密
 */
router.post('/changePassword', function (req, res) {
  // 检查每个参数是否有值
  if (objectValueAllEmpty(req.body)) {
    res.send({
      errno: 202,
      message: '参数错误'
    })
    return
  }

  const {newPassword, username} = req.body;
  UsersModel.updateOne({ username }, {password:newPassword},function (err, {matchedCount}) {
    if (err) {
      res.send({
        errno: 201,
        message: err.message
      })
    } else if (matchedCount === 0) {
      res.send({
        errno: 202,
        message: '参数值错误'
      })
    } else {
      res.send({
        errno: 0,
        message: 'success'
      })
    }
  })
})

module.exports = router;
